<template>
  <div class="spotCheck">
    <listTableNew v-bind="tableSetting" ref="klTable"></listTableNew>
  </div>
</template>

<script>
import {
  getListApi //列表
} from "@/api/orderManagement/spotCheck";

// import { queryPageList } from "@/api/orderManagement/orderCommon";
export default {
  props: {},
  data() {
    return {
      userInfo: this.$store.getters.userInfo,
      minDate:"",
      maxDate:"",
      tableSetting: {
        radioShow: false,
        showCustomBtn: true, //是否显示自定义列按钮
        showFilterBtn: true, //是否显示过滤器按钮
        pageConfig: {
          pageCode: "spotCheck", //页面标识，用来存储过滤器以及自定义列的时候用
          userId: this.$store.getters.userInfo.id
        },
        // 查询配置
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params, sortStr);
          },
          queryData: {
            page: 1,
            pageSize: 15
          },
          // 查询条件
          searchArr: [
            {
              type: "select",
              label: "工单类型",
              model: "orderTypeList",
              multiple: true,
              option: this.$store.getters.dirtData("order_type")
            },
            {
              type: "input",
              label: "编号",
              model: "orderCode"
            },
            {
              type: "input",
              label: "标题",
              model: "title"
            },
            {
              type: "dept",
              label: "设备管理单位",
              model: "deptManageName",
              idsStr: "deptManage"
            },
            // {
            //   type: "device",
            //   label: "设备",
            //   model: "equipmentName",
            //   idsStr: "equipmentId"
            // },
            {
              label: "创建时间",
              model: "createSection",
              type: "datetimerange",
              pickerOptions: {
                onPick: ({ maxDate, minDate }) => {
                  this.minDate = minDate;
                  this.maxDate = maxDate;
                },
                disabledDate: time => {
                  //查询时间跨度为31天
                  if (this.minDate) {
                    let range = 31 * 24 * 3600 * 1000;
                    return (
                      time.getTime() > Date.now() ||
                      time.getTime() > this.minDate.getTime() + range ||
                      time.getTime() < this.minDate.getTime() - range
                    );
                  }
                  return time.getTime() > Date.now();
                }
              }
            },
            {
              type: "select",
              label: "抽查结果",
              model: "inspectResult",
              option: [
                {
                  label: "合格",
                  value: 1
                },
                {
                  label: "不合格",
                  value: 0
                }
              ]
            }
          ]
        },
        // 按钮设置
        actionList: [],
        // 表格的行
        column: [
          {
            label: "状态",
            prop: "status",
            formatter: row => {
              return this.$store.getters["fineDirtDataName"](
                "order_all_status",
                row.status
              );
            },
            width: "110px"
          },
          {
            label: "编号",
            prop: "code",
            width: "200px"
          },
          {
            label: "标题",
            prop: "title",
            width: "320px",
            showTooltip: true,
          },
          {
            label: "设备管理单位",
            prop: "deptManageName",
            minWidth: "180px",
            showTooltip: true,
          },
          {
            label: "设备",
            prop: "equipmentName",
            minWidth: "180px",
            showTooltip: true,
          },
          {
            label: "工单类型",
            prop: "orderType",
            formatter: row => {
              return this.$store.getters["fineDirtDataName"](
                "order_type",
                row.orderType
              );
            },
            minWidth: "100px"
          },
          {
            label: "创建人",
            prop: "createByName",
            minWidth: "90px"
          },
          {
            label: "创建时间",
            prop: "createTime",
            minWidth: "140px"
          },
          {
            label: "抽查结果",
            prop: "inspectResult",
            formatter: row => {
              let value = row.inspectResult;
              let name = "-";
              if (value == "1") {
                name = "合格";
              } else if (value == "0") {
                name = "不合格";
              }
              return name;
            },
            minWidth: "90px"
          }
        ],
        // 表格数据
        tableData: [],
        //表格方法
        tableMethods: {
          //点击行的方法
          rowClick: row => {
            this.gotoDetails(row, "check");
          }
        }
      }
    };
  },
  created() {},
  mounted() {
    this.init();
  },
  methods: {
    init() {
      let endDate = this.$utils.getCurrentDate("minutes");
      let day = new Date() - 1000 * 60 * 60 * 24 * 7;
      let startDate = this.$utils.TimeFormat(day, 3);
      /* 初始化查询条件 */
      this.$set(this.tableSetting.searchObj.queryData, "createSection", [
        startDate,
        endDate
      ]);
      this.$set(this.tableSetting.searchObj.queryData, "orderTypeList", [
        "defect"
      ]);
      this.$set(
        this.tableSetting.searchObj.queryData,
        "deptManageName",
        this.userInfo.deptName
      );
      setTimeout(() => {
        this.$refs.klTable.getTableConfig();
        // this.updateData();
      }, 1000);
    },
    updateData() {
      this.$refs.klTable.searchData();
    },
    getTableData(params, sortStr) {
      let form = { ...params };
      if (this.orderCode) {
        form.orderCode = this.orderCode;
      }
      if (sortStr) {
        form.orders = sortStr;
      }
      getListApi(form)
        .then(res => {
          let data = res.data.list || [];
          let total = res.data.total || 0;
          this.$set(this.tableSetting, "tableData", data);
          this.$set(this.tableSetting, "totalNumber", total);
        })
        .catch(e => {
          this.$set(this.tableSetting, "tableData", []);
          this.$set(this.tableSetting, "totalNumber", 0);
        });
    },
    gotoDetails(row, type) {
      let form = {
        pageType: type,
        id: row.id,
        status: row.status
      };
      let routerName = "";
      switch (row.orderType) {
        //巡视
        case "patrol":
          routerName = "patrolOrderDetails";
          break;
        //计划检修
        case "repair":
          routerName = "planRepairDetails";
          break;
        //试验
        case "experiment":
          routerName = "experimentOrderDetails";
          break;
        //大修
        case "overhaul":
          routerName = "overhaulDetails";
          break;
        //正常维护
        case "maintain":
          routerName = "normalMaintenanceDetails";
          break;
        //缺陷工单
        case "defect":
          routerName = "defectOrderDetails";
          break;
      }
      if (routerName) {
        const { href } = this.$router.resolve({
          name: routerName,
          query: form
        });
        window.open(href, "_blank");
      }
    },
  }
};
</script>

<style lang="less" scoped>
.spotCheck {
  min-height: 500px;
  height: 100%;
}
</style>